<?php

class Modul {

    // --- ATTRIBUTES ---

    /**
     * ModulID from current Modul
     * @var Integer
     */
    private $modulID;

    /**
     * Modulname from current Modul
     * @var String
     */
    private $modulName;

    /**
     * Array with fix texts from language directory
     * @var Array
     */
    private $textArray = array();

    /**
     * Boolean if Modul is boxed or not
     * @var Integer[1]
     */
    private $isBoxed;

    /**
     * Boolean if Modul is activeted or not
     * @var Integer[1]
     */
    private $isActiveted;

    /**
     * Parameter from Sites to Modul
     * @var String
     */
    private $siteParameter = "";

    // --- OPERATIONS ---

    /**
     * Constructor of current class
     * check for valid ModulID and set $modulID and $modulName
     *
     * @param  Integer ModulID
     */
    public function __construct($passedID) {
        global $MySQL_DB;

        if($passedID > 0) {
            // check if modul exists and read name
            $res_check_modul = execute_query("SELECT name, boxed, activeted FROM %PRE%module WHERE ID = ".$passedID,$MySQL_DB);
            if(!($check_modul = $res_check_modul->fetch_object())) {
                write_errorlog("Selected modul not in database ID: ".$passedID,1,"Modul");
            }
            else {
                $this->modulName = $check_modul->name;
                $this->isBoxed = $check_modul->boxed;
                $this->isActiveted = $check_modul->activeted;
            }
        }

        $this->modulID = $passedID;

        $this->setTextArray();
    }

    public function __destruct() {
        foreach($this as $key => $value) {
            unset($this->$key);
        }
    }

    /**
     * includes current Modul
     */
    public function displayModul() {
        global $MySQL_DB;
        global $indexLanguage;
        global $currentUser;
        global $indexModul;

        if($this->isActiveted == 1) {
            // check if modul enabled for current user
            $flag_echo_modul = 0;

            // get all usergroups
            $usergroups = $currentUser->getAllUsergroups();
            foreach($usergroups as $usergroupID) {
                // check if module enabled for usergroup
                $res_check_module = execute_query("SELECT module_ID FROM %PRE%usergroups_has_module WHERE usergroups_ID = ".$usergroupID." and module_ID = ".$this->modulID,$MySQL_DB);
                if($check_module = $res_check_module->fetch_object()) {
                    $flag_echo_modul = 1;
                }
            }

            if($flag_echo_modul == 1) {
                // check if file exists
                if(file_exists('module/'.$this->modulName.'/index.php')) {
                    include('module/'.$this->modulName.'/index.php');
                }
                else {
                    write_errorlog("No file/directory for Modul: ".$this->modulName,3,"Modul");
                }
            }
        }
    }

    /**
     * echo stylesheets from current Modul
     */
    public function echoStylesheets() {
        // index page ?
        if($this->modulID == 0) {
            // check if file exists
            if(file_exists('css/index.php')) {
                // include stylesheet list
                include('css/index.php');

                foreach($stylesheet as $cssfile) {
                    echo '<link href="css/'.$cssfile.'.css" rel="stylesheet" type="text/css">'."\n";
                }
            }
        }
        else {
            // check if file exists
            if(file_exists('module/'.$this->modulName.'/css/index.php')) {
                // include stylesheet list
                include('module/'.$this->modulName.'/css/index.php');

                foreach($stylesheet as $cssfile) {
                    echo '<link href="module/'.$this->modulName.'/css/'.$cssfile.'.css" rel="stylesheet" type="text/css">'."\n";
                }
            }
        }
    }

    /**
     * Sets $textArray from language file
     */
    private function setTextArray() {
        global $indexLanguage;

        if($this->modulID == 0)
            $filepath = 'language/';
        else
            $filepath = 'module/'.$this->modulName.'/language/';

        $languageID = $indexLanguage->getLanguageID();
        $defaultLanguageID = $indexLanguage->getDefaultLanguageID();

        // read users language module
        if(file_exists($filepath.$languageID.'.php'))
            $module_file = $filepath.$languageID.'.php';
        // read default language module
        elseif(file_exists($filepath.$defaultLanguageID.'.php'))
            $module_file = $filepath.$defaultLanguageID.'.php';
        // use english language module
        elseif(file_exists($filepath.'en.php'))
            $module_file = $filepath.'en.php';
        else {
            write_errorlog('No language modul: '.$filepath,3,$this->modulName);
        }

        if(isset($module_file)) {
            include($module_file);
            $this->textArray = array();

            foreach($language_array as $key => $val) {
                $this->textArray[$key] = $val;
            }
        }
    }

    /**
     * Gets template from file and returns it
     * replace static text and variable in template
     *
     * @param  String $html -> Name of the HTML-File
     * @param  Array $template_vars -> Array with variables
     * @return String -> converted HTML-File
     */
    public function getTemplate($html,$template_vars) {

        if($this->modulID == 0)
            $filepath = 'template/'.$html.'.html';
        else
            $filepath = 'module/'.$this->modulName.'/template/'.$html.'.html';


        // check if template available
        if(file_exists($filepath)) {
            $template = file_get_contents($filepath);

            // replace static text
            foreach($this->textArray as $key => $val) {
                $template = str_replace('%'.$key.'%', $val, $template);
            }
            // replace variable
            foreach($template_vars as $key => $val) {
                $template = str_replace('$'.$key.'$', $val, $template);
            }
            return $template;
        }
        else {
            write_errorlog('Template does not exists: '.$filepath,2,$this->modulName);
        }
    }

    /**
     * Returns text from $textArray at passed Key
     *
     * @param  String $key -> Assoc-Array Key
     * @return String -> Text
     */
    public function getText($key) {
        // check if key exists
        if(array_key_exists($key, $this->textArray)) {
            return $this->textArray[$key];
        }
        else {
            write_errorlog('Key does not exists in TextArray: '.$key,3,$this->modulName);
            return $key;
        }
    }

    // --- GET/SET METHODS --

    public function getModulID() {
        return $this->modulID;
    }

    public function getModulName() {
        return $this->modulName;
    }

    public function setSiteParameter($pSiteParameter) {
        $this->siteParameter = $pSiteParameter;
    }

}

?>